/* 
 * File:   eph_table.cpp
 * Author: gian
 * 
 * Created on January 30, 2013, 2:19 PM
 */

#include <stdio.h>
#include <assert.h>
#include <iostream>
#include "eph_table.hpp"

eph_table::eph_table(const std::string& filename, double time_start, double time_end)
{
    mTimeStart = time_start;
    mTimeEnd = time_end;
    
    FILE *in_file = fopen(filename.c_str(),"rb");
    eph_table_entry tp;

    while( !feof(in_file) ) {
        fread((void*)&tp,sizeof(eph_table_entry),1,in_file);
        if( (tp.t>=time_start) && (tp.t<=time_end) ) {
            mTable.push_back(tp);
        }
    }
    fclose(in_file);
    mTimeStart = mTable.front().t;
    mTimeEnd = mTable.back().t;
    mTimeStep = mTable[1].t-mTable[0].t;
}

eph_table::~eph_table() 
{

}

void eph_table::get(int i, eph_table_entry& interpolated)
{
    if( (i<0) || (i>=mTable.size())) {
        std::cerr << "bad index" << std::endl;
        throw;

    }
    interpolated = mTable[i];
}

void eph_table::get(eph_table_entry& interpolated)
{
    int k = int((interpolated.t-mTimeStart)/mTimeStep);
    if( (k<0) || (k>=mTable.size())) {
        std::cerr << "time outside range" << std::endl;
        throw;
    }
    double dt = interpolated.t-mTable[k].t;

    
    
}